package com.hspedu.newData.dmsxl.树.搜索树;

import com.hspedu.newData.dmsxl.ds.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-09 
 * @Version: 1.0
 */

public class LC98验证二叉搜索树 {

    // todo 不会
    public boolean isValidBST(TreeNode root) {

        if (root == null){
            return true;
        }

        boolean leftTree = true;
        boolean rightTree = true;
        if (root.left != null && root.val < root.left.val){
            leftTree = false;
        }
        if (root.right != null && root.val > root.right.val){
            rightTree = false;
        }

        return leftTree && rightTree && isValidBST(root.left) && isValidBST(root.right);
    }


    // todo 效率很低
    public boolean isValidBST1(TreeNode root) {
        List<Integer> list1 = inorderTraversal(root);
        for (int i = 1; i < list1.size(); i++) {
            if (list1.get(i) <= list1.get(i-1)){
                return false;
            }
        }
        return true;

    }

    private List<Integer> list = new ArrayList<>();

    public List<Integer> inorderTraversal(TreeNode root) {
        if (root == null){
            return list;
        }

        inorderTraversal(root.left);
        list.add(root.val);
        inorderTraversal(root.right);

        return list;
    }
}
